package com.enhe.gck.fg;

import cn.hutool.core.util.StrUtil;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.communal.PublicReturn;
import com.enhe.gck.pojo.WovenBag;
import com.enhe.gck.pojo.WovenBagRecord;
import com.kuangkie.carbon.fg.*;
import com.kuangkie.carbon.fg.ops.FGOpsComplexus;
import com.kuangkie.carbon.fg.ops.ProRecordOps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import com.kuangkie.carbon.record.FGRecordComplexus;
import com.kuangkie.carbon.record.ProRecord;
import com.enhe.gck.CarbonBaseConstant; //模型Code
import com.enhe.gck.CarbonEnumKeyValue; //枚举字典信息
import com.enhe.gck.CarbonRelationType; //关系类型Code
import com.enhe.gck.item.WovenBagHWANE21190Item; // 模型属性Code

import java.time.LocalDateTime;
import java.util.Collection;

@Repository(value = "hwane21190366042330866425862")
@Slf4j
public class WovenBagHWANE21190BNB implements FirstRoundImproveFuncGroup,SecondRoundImproveFuncGroup {
	
	@Override
	public synchronized void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
			ImproveResultFactory improveResultFactory) {
		WovenBag info=proRecord.getBean(WovenBag.class);
		if(proRecord.isNew()){
			PublicMethod.saveAddNow(improveResultFactory).putAttribute(WovenBagHWANE21190Item.基本属性组_数量,info.getReceive());

			if(StrUtil.isNotBlank(context.getUserCode())){
				PublicMethod.saveAddNow(improveResultFactory).addRelation(CarbonRelationType.RR_编织袋管理_创建账户_系统用户,context.getUserCode());
			}
		}
		if(info.getIsDelete()==null||!info.getIsDelete()){
//			WovenBag infoOld=PublicMethod.getInfo(CarbonBaseConstant.编织袋管理_bzdgl, info.getRecordCode(), WovenBag.class);

			if(String.valueOf(CarbonEnumKeyValue.操作命令_编织袋管理_确认_bzdglqr).equals(info.getCommand())&&info.getIsUsing()!=null&&info.getIsUsing()){
//				if(infoOld!=null&&infoOld.getIsUsing()!=null&&infoOld.getIsUsing()){
//					PublicMethod.returnMessage(improveResultFactory, PublicReturn.WovenBag_1);
//					return;
//				}
				if(StrUtil.isNotBlank(context.getUserCode())){
					PublicMethod.saveAddNow(improveResultFactory).addRelation(CarbonRelationType.RR_编织袋管理_确认账户_系统用户,context.getUserCode());
				}
				PublicMethod.saveAddNow(improveResultFactory).putAttribute(WovenBagHWANE21190Item.基本属性组_确认时间, LocalDateTime.now());
			}

			if(String.valueOf(CarbonEnumKeyValue.操作命令_编织袋管理_剩余确认_bzdglsyqr).equals(info.getCommand())&&info.getIsConfirmRemain()!=null&&info.getIsConfirmRemain()){
//				if(infoOld!=null&&infoOld.getIsConfirmRemain()!=null&&infoOld.getIsConfirmRemain()){
//					PublicMethod.returnMessage(improveResultFactory, PublicReturn.WovenBag_1);
//					return;
//				}

				if(StrUtil.isNotBlank(context.getUserCode())){
					PublicMethod.saveAddNow(improveResultFactory).addRelation(CarbonRelationType.RR_编织袋管理_剩余确认账户_系统用户,context.getUserCode());
				}
				PublicMethod.saveAddNow(improveResultFactory).putAttribute(WovenBagHWANE21190Item.基本属性组_剩余确认时间, LocalDateTime.now());

				Collection<String> listR=proRecord.getGuestRecordCodes(CarbonRelationType.RR_编织袋管理_交接班次_编织袋管理);
				if(listR!=null&&!listR.isEmpty()){
					int sy=0;
					for (String rId:listR) {
						if(info.getRecordCode().equals(rId)){
							PublicMethod.returnMessage(improveResultFactory, PublicReturn.WovenBag_2);
							return;
						}

						WovenBag wovenBag=PublicMethod.getInfo(CarbonBaseConstant.编织袋管理_bzdgl, rId, WovenBag.class);
						if(wovenBag!=null&&wovenBag.getIsConfirmTransmit()!=null&&!wovenBag.getIsConfirmTransmit()){

							sy=sy+wovenBag.getSurplus();
							PublicMethod.updateAny(improveResultFactory,CarbonBaseConstant.编织袋管理_bzdgl,wovenBag.getRecordCode())
									.putAttribute(WovenBagHWANE21190Item.基本属性组_转交确认, true);
						}
					}
					PublicMethod.saveAddNow(improveResultFactory)
							.putAttribute(WovenBagHWANE21190Item.基本属性组_数量, info.getNumber()+sy)
							.putAttribute(WovenBagHWANE21190Item.基本属性组_剩余数, info.getSurplus()+sy)
							.putAttribute(WovenBagHWANE21190Item.基本属性组_前班次转交数, sy)
					;
				}
			}
		}
	}


	@Override
	public void preImprove(FuncGroupContext funcGroupContext, ProRecord proRecord, ProRecordOps proRecordOps, FGRecordComplexus fgRecordComplexus, FGOpsComplexus fgOpsComplexus, ImproveResultFactory improveResultFactory) {

	}

	@Override
	public void improve(FuncGroupContext funcGroupContext, ProRecord proRecord, FGRecordComplexus fgRecordComplexus, ImproveResultFactory improveResultFactory) {
		WovenBag info=proRecord.getBean(WovenBag.class);
		if(proRecord.isNew()){
			PublicMethod.saveAddNow(improveResultFactory)
					.putAttribute(WovenBagHWANE21190Item.基本属性组_数量,info.getReceive())
					.putAttribute(WovenBagHWANE21190Item.基本属性组_剩余数,info.getReceive())
			;
		}
	}

	@Override
	public void postImprove(FuncGroupContext funcGroupContext, ProRecord proRecord, FGRecordComplexus fgRecordComplexus, ImproveResultFactory improveResultFactory) {

	}
}
